<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
    height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */


</style>

<template>
    <div class="employee-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="search">
                <span>快速搜索</span>
                <Input v-model="searchForm.name" placeholder="请输入员工姓名" style="width: 140px;margin-bottom:10px;"></Input>
                <Input v-model="searchForm.no" placeholder="请输入员工工号" style="width: 140px;margin-bottom:10px;"></Input>
                <Select v-model="searchForm.istry" style="width: 140px;margin-bottom:10px;" placeholder='请选择类型'>
                    <Option value="0" >全部</Option>
                    <Option value="1" >正式员工</Option>
                    <Option value="2" >非正式员工</Option>
                </Select>
                <Select v-model="searchForm.rank" style="width:140px;margin-bottom:10px;" placeholder='请选择级别'>
                    <Option value="0" >全部</Option>
                    <Option :value="item.id" v-for="item in rankList" :key="item.id" >{{item.name}}</Option>
                </Select>
                <Select v-model="searchForm.sex" style="width: 140px;margin-bottom:10px;" placeholder='请选择性别'>
                    <Option value="0" >全部</Option>
                    <Option value="1" >男</Option>
                    <Option value="2" >女</Option>
                </Select>
                <Select v-model="searchForm.dept" style="width: 140px;margin-bottom:10px;" placeholder='请选择部门'>
                    <Option value="0" >全部</Option>
                    <Option :value="item.id" v-for="item in deptList" :key="item.id" >{{item.name}}</Option>
                </Select>
                <Button @click='search'style="margin-bottom:10px;">查询</Button>             
            </div>
            <div class="detail">
                <div class="headline">
                    
                    <Button class="add-agent" @click='showAdd(-1)'>新增员工</Button>
                </div>
                <div class="clearfix">
                    <Table border :columns="columns_employee" :data="data_employee"></Table>
                    <Row class="page fr">
                        <Col span="24">
                            <Page ref="pages" :total="employeeTotal" :current="currentpage" show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                        </Col>
                    </Row>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { getEmployeeList, getEmployeeDetail, saveEmployee, delEmployee , updateEmployee  , getAllEmployee} from '../../api/employee'
import { getRankList} from '../../api/rank'
import { getRoleList} from '../../api/role'
import { getDeptList} from '../../api/dept'
export default {
    data() {
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.employeeDetail.pwd_two !== '') {
                    // 对第二个密码框单独验证
                    this.$refs.employeeDetail.validateField('pwd_two');
                }
                callback();
            }
        };
        const validatePassCheck = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.employeeDetail.password) {
                callback(new Error('两次输入的密码不匹配!'));
            } else {
                callback();
            }
        };
        return {
            currentpage:1,
            cou:-1,
            checkModal:false,
            closeable: false,
            loading: true,
            page: 1,
            pagesize: 10,
            searchForm: {
                name: '',
                no: '',
                sex:'',
                istry:'',
                rank:'',
                dept:''
            },
            employeeDetail: {
                job_num: '',
                name: '',
                rank: '',
                dept:'',
                role:'',
                date:'',
                datetime:'',
                join_time:'',
                promotion_time:'',
                join_team_time:'',
                mobile:'',
                sex:'',
                rec_uid:'',
                superior:'',
                login_name:'',
                password:'',
                pwd_two:'',
            },
            showAddModal: false,
            rankList: [
    
            ],
            roleList:[

            ],
            deptList:[

            ],
            employeeList:[

            ],
            employeeTotal: 0,
            columns_employee: [
                {
                    title: '工号',
                    key: 'job_num',
                    align: 'center',
                },
                {
                    title: '姓名',
                    key: 'name',
                    align: 'center',
                },
                {
                    title: '性别',
                    key: 'sex_name',
                    align: 'center',
                },
                {
                    title: '手机号',
                    key: 'mobile',
                    align: 'center',
                    render: (h, params) => {                                               
                        return h('div', {                                                       
                        },params.row.mobile != 0 ? params.row.mobile : '未知');
                        
                    }
                },
                 {
                    title: '部门',
                    key: 'dept_name',
                    align: 'center',
                },
                 {
                    title: '职级',
                    key: 'rank_name',
                    align: 'center',
                },
                {
                    title: '角色',
                    key: 'role_name',
                    align: 'center',
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 120,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showAdd(params.row.id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delConfirm(params.index)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                },
            ],
            data_employee: [

            ],
            ruleValidate: {
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                job_num: [
                    { required: true, message: '员工编号不能为空', trigger: 'blur' },
                ],
                rank: [
                    { required: true, message: '请选择级别', trigger: 'blur,change' }
                ],
                password: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                pwd_two: [
                    { validator: validatePassCheck, trigger: 'blur' }
                ],
                login_name: [
                    { required: true, message: '登录名不能为空', trigger: 'blur' },
                ],
            },
            nowdate:''
        }
    },
    components: {
        productTitle
    },
    created() {
        
    },
    mounted () {
        console.log(this.$route.query) 
        if(Object.keys(this.$route.query).length == 0){
            //console.log(123)
            this.$router.push({name:'employee_list',query:{p:1}})
            this.currentpage = 1
        }else{
            this.currentpage = parseInt(this.$route.query.p)
            this.searchForm = Object.assign({},this.searchForm,this.$route.query) 
        }
        this.getList()
        this.getRank()
        this.getRole()
        this.getDept()
        this.getAllEmployee()
    },
    methods: {
 
        Format(time) { 
            let fmt;
            let _year = time.getFullYear()
            let _month = time.getMonth() + 1
            let _date = time.getDate()
            if(_month < 10){
                _month = '0'+_month
            }
            if(_date<10){
                _date = '0'+_date
            }
            fmt = _year +'-'+ _month +'-'+ _date
            return fmt;
            
        },
        // 搜索
        search() {
            this.page = 1
            let _query = Object.assign({},this.searchForm,{p:1,pagesize: this.pagesize})
            this.$router.push({name:'employee_list',query:_query})            
            this.getList(-1) 
            
                    
        },
        // 获取员工列表
        getList(id) {
            let _params = {
                'p': this.$route.query.p,
                'pagesize': this.pagesize,
                'name': this.searchForm.name,
                'job_num': this.searchForm.no,
                'rank': this.searchForm.rank,
                'dept':this.searchForm.dept,
                'sex':this.searchForm.sex,
                'istry':this.searchForm.istry
            }
            new Promise((resolve, reject) => {
                getEmployeeList(_params).then(response => {
                    console.log(response.data);
                    if (response.data.err == 0) {
                        this.data_employee = response.data.data.list;
                        this.employeeTotal = response.data.data.total;
                        if(id == -1){
                            this.$refs['pages'].currentPage = 1 
                        }
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取全部员工
        getAllEmployee() {
            new Promise((resolve, reject) => {
                getAllEmployee().then(response => {
                    console.log(response.data)
                    if (response.data.err == 0) {
                        this.employeeList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取职级列表
        getRank() {
            new Promise((resolve, reject) => {
                getRankList().then(response => {
                    if (response.data.err == 0) {
                        this.rankList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取角色列表
        getRole() {
            let _params = {
                cate:1
            }
            new Promise((resolve, reject) => {
                getRoleList(_params).then(response => {
                    if (response.data.err == 0) {
                        this.roleList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //获取部门列表
        getDept() {
            new Promise((resolve, reject) => {
                getDeptList().then(response => {
                    if (response.data.err == 0) {
                        this.deptList = response.data.data;
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 页码改变
        pageChange(page) {
            this.page = page
            console.log(page)
            let _query = Object.assign({},this.searchForm,{p:page,pagesize: this.pagesize})
            console.log(_query)
            this.$router.push({name:'employee_list',query:_query})
            this.getList()
        },
        // 每页条数改变
        pageSizeChange(pagesize) {
            this.pagesize = pagesize
            this.getList();
        },

        del(param) {
            new Promise((resolve, reject) => {
                delEmployee(param).then(response => {
                    
                    if (response.data.err == 0) {
                        this.getList();
                        this.$Message.success(response.data.info);
                    }else{
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //添加or编辑代理人，打开modal
        showAdd(index){
            if(index > 0){
                this.$router.push({'name':'employee_edit',params:{id:index}})
            }else{
                this.$router.push({'name':'employee_create'})
            }
        },
        // 删除时弹出对话框
        delConfirm(index) {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该代理人?',
                onOk: () => {
                    let _params = {
                        'id': this.data_employee[index].id
                    }
                    this.del(_params);
                },
            });
        },

    }
}
</script>
